package com.jay.nio.clazz;

import java.io.IOException;
import java.nio.ByteBuffer;

public class ByteBufferCls {
	public static void main(String[] args) throws IOException {
		// 创建一个capacity为256的ByteBuffer
		ByteBuffer buf = ByteBuffer.allocate(256);
		System.out.println("is direct? " + buf.isDirect());
		System.out.println(buf.array().length);
		System.out.println(buf.hasArray());
		System.out.println(buf.isReadOnly());
		System.out.println(buf.order());

		System.out.println("-----------------------------");
		buf.putChar('a');
		buf.putChar('b');
		buf.putChar('c');
		buf.putChar('d');
		buf.putChar('e');
		buf.putChar('f');
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());
		buf.flip();
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());

		byte[] content = new byte[buf.limit()];
		buf.get(content);
		// 把byte数组的内容写到标准输出
		System.out.print(new String(content));
		System.out.println();
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());
		System.out.println(buf.arrayOffset());

		buf.clear();

		System.out.println("-----------------------------");
		buf.putChar('z');
		buf.putChar('x');
		// rewind 不重设limit，所以以前剩余的字符也会输出
		// buf.rewind();
		buf.flip();
		content = new byte[buf.limit()];
		buf.get(content);
		// 把byte数组的内容写到标准输出
		System.out.print(new String(content));
		System.out.println();
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());

		buf.clear();
		System.out.println("-----------------------------");
		buf.position(2);
		buf.putChar('p');
		buf.putChar('o');
		buf.putChar('i');
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());
		buf.flip();
		buf.position(2);
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());
		// 比较复杂，可参见实现代码
		buf.compact();
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());

		System.out.println("-----------------------------");
		// 复制
		ByteBuffer bufDup = buf.duplicate();
		System.out.println(bufDup.position() + "/" + bufDup.limit() + "/"
				+ bufDup.capacity());

		// position后面的子buff
		ByteBuffer bufSlice = buf.slice();
		System.out.println(bufSlice.position() + "/" + bufSlice.limit() + "/"
				+ bufSlice.capacity());

		// position后面的子buff
		ByteBuffer bufWrap = ByteBuffer.wrap("lkjhg".getBytes());
		System.out.println(bufWrap.position() + "/" + bufWrap.limit() + "/"
				+ bufWrap.capacity());

		System.out.println("-----------------------------");
		ByteBuffer bufD = ByteBuffer.allocateDirect(256);
		System.out.println("is direct? " + bufD.isDirect());
		System.out.println(bufD.hasArray());
		System.out.println(bufD.isReadOnly());
		System.out.println(bufD.order());

		bufD.putChar('d');
		bufD.putChar('i');
		bufD.putChar('r');
		bufD.putChar('e');
		bufD.putChar('c');
		bufD.putChar('t');
		System.out.println(bufD.position() + "/" + bufD.limit() + "/"
				+ bufD.capacity());
		bufD.flip();
		System.out.println(bufD.position() + "/" + bufD.limit() + "/"
				+ bufD.capacity());

		content = new byte[bufD.limit()];
		bufD.get(content);
		// 把byte数组的内容写到标准输出
		System.out.print(new String(content));
		System.out.println();
		System.out.println(bufD.position() + "/" + bufD.limit() + "/"
				+ bufD.capacity());

		bufD.clear();
	}

}
